package com.ysd.lis.controller.sys;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.dto.InterfaceExtParamDto;
import com.ysd.lis.entity.SysInterfaceDictParam;
import com.ysd.lis.entity.SysInterfaceMethExtParam;
import com.ysd.lis.service.SysInterfaceDictParamService;
import com.ysd.lis.service.SysInterfaceMethExtParamService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 接口场景外源语句参数对照 前端控制器
 * </p>
 *
 * @author cfz
 * @since 2024-04-26
 */
@RestController
@RequestMapping("/api/sys/sys-interface-meth-ext-param")
public class SysInterfaceMethExtParamController extends BaseController {
    @Autowired
    SysInterfaceMethExtParamService sysInterfaceMethExtParamService;
    @Autowired
    SysInterfaceDictParamService sysInterfaceDictParamService;

    @ApiOperation("查询")
    @GetMapping("/findSysInterfaceMethExtParamList")
    public Result findSysInterfaceMethExtParamList(SysInterfaceMethExtParam sysInterfaceMethExtParam) {
        return sysInterfaceMethExtParamService.findSysInterfaceMethExtParamList(sysInterfaceMethExtParam);
    }

    @ApiOperation("增加")
    @PostMapping("/addSysInterfaceMethExtParam")
    public Result addSysInterfaceMethExtParam(@RequestBody SysInterfaceMethExtParam sysInterfaceMethExtParam) {
        return sysInterfaceMethExtParamService.addSysInterfaceMethExtParam(sysInterfaceMethExtParam);
    }

    @ApiOperation("删除")
    @PostMapping("/deleteSysInterfaceMethExtParam")
    public Result deleteSysInterfaceMethExtParam(@RequestBody SysInterfaceMethExtParam sysInterfaceMethExtParam) {
        return sysInterfaceMethExtParamService.deleteSysInterfaceMethExtParam(sysInterfaceMethExtParam);
    }

    @ApiOperation("批量删除")
    @PostMapping("/batchDeleteSysInterfaceMethExtParam")
    public Result batchDeleteSysInterfaceMethExtParam(@RequestBody List<SysInterfaceMethExtParam> sysInterfaceMethExtParams) {
        return sysInterfaceMethExtParamService.batchDeleteSysInterfaceMethExtParam(sysInterfaceMethExtParams);
    }

    @ApiOperation("批量操作，更新、新增")
    @PostMapping("/batchSaveSysInterfaceMethExtParam")
    public Result batchSaveSysInterfaceMethExtParam(@RequestBody List<SysInterfaceMethExtParam> sysInterfaceMethExtParams) {
        return sysInterfaceMethExtParamService.batchSaveSysInterfaceMethExtParam(sysInterfaceMethExtParams);
    }

    @ApiOperation("修改")
    @PostMapping("/updateSysInterfaceMethExtParam")
    public Result updateSysInterfaceMethExtParam(@RequestBody SysInterfaceMethExtParam sysInterfaceMethExtParam) {
        return sysInterfaceMethExtParamService.updateSysInterfaceMethExtParam(sysInterfaceMethExtParam);
    }

    @ApiOperation("执行查询")
    @PostMapping("/execExtParam")
    public JSONObject execExtParam(@RequestBody InterfaceExtParamDto dto) {
        return sysInterfaceMethExtParamService.execExtParam(dto);
    }

    @ApiOperation("同步WEB入参")
    @GetMapping("/synchronousParam")
    public Result synchronousParam(String interfaceDictId,String interfaceMethodId,String interfaceId) {

        //删除掉所有的配置
        LambdaQueryWrapper<SysInterfaceMethExtParam> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(SysInterfaceMethExtParam::getInterfaceMethodId,interfaceMethodId);
        sysInterfaceMethExtParamService.remove(lambdaQueryWrapper1);


        LambdaQueryWrapper<SysInterfaceDictParam> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceDictParam::getDictId,interfaceDictId)
                .eq(SysInterfaceDictParam::getType,1)
                .eq(SysInterfaceDictParam::getDelFlag,0);
        List<SysInterfaceDictParam> list = sysInterfaceDictParamService.list(lambdaQueryWrapper);
        List<SysInterfaceMethExtParam> list1 = new ArrayList<>();
        list.forEach(a->{
            SysInterfaceMethExtParam sysInterfaceMethExtParam = new SysInterfaceMethExtParam();
            sysInterfaceMethExtParam.setParamParentId(a.getParentId());
            sysInterfaceMethExtParam.setParamId(a.getId());
            sysInterfaceMethExtParam.setInCodeId(a.getId());
            sysInterfaceMethExtParam.setInCodeNa(a.getCommon());
            sysInterfaceMethExtParam.setInCodeDtype(a.getColoumType()+"");
            sysInterfaceMethExtParam.setInterfaceMethodId(interfaceMethodId);
            sysInterfaceMethExtParam.setType(1);
            sysInterfaceMethExtParam.setSymbol("equal");
            sysInterfaceMethExtParam.setInCodeNo(a.getCode());
            sysInterfaceMethExtParam.setInterfaceId(interfaceId);
            list1.add(sysInterfaceMethExtParam);
        });
        boolean b = sysInterfaceMethExtParamService.saveBatch(list1);
        if(b){
            return Result.succ(1,"更新成功","");
        }else{
            return Result.succ(400,"更新成功","");
        }
    }
}
